package com.example.springsecuritydemo.support;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.io.Serializable;

/**
 * 存放用户id和用户名
 * @author chenggang
 * @date 2019/04/09
 *
 */
public class UserContext implements Closeable{

	private static final Logger logger = LoggerFactory.getLogger(UserContext.class);
	
	
	public static  ThreadLocal<User> userContext = new ThreadLocal<User>();
	
	public static void set(User user) {
		userContext.set(user);
	}
	
	public static User get() {
		return userContext.get();
	}
	
	public static void remove(){
		userContext.remove();
    }
	
	@Override
	public void close() throws IOException {
		logger.info("close user threadLocal");
		userContext.remove();
	}
	

	@Data
	public class User implements Serializable{

		private static final long serialVersionUID = 4374014400604313411L;

		private Integer userId;
		
		private String userAccount;
		
		private String userName;
		
	}

	
}
